<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Create or open shared memory block</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.shmop-delete.html">« shmop_delete</a></li>
      <li style="float: right;"><a href="function.shmop-read.html">shmop_read »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.shmop.html">Shared Memory 函数</a></li>
    <li>Create or open shared memory block</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.shmop-open" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">shmop_open</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">shmop_open</span> &mdash; <span class="dc-title">Create or open shared memory block</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.shmop-open-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>shmop_open</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$key</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$mode</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$permissions</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$size</code></span><br>): <span class="type"><span class="type"><a href="class.shmop.html" class="type Shmop">Shmop</a></span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>shmop_open()</strong></span> can create or open a shared memory block.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.shmop-open-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">key</code></dt>

     <dd>

      <p class="para">
       System&#039;s id for the shared memory block.
       Can be passed as a decimal or hex.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">mode</code></dt>

     <dd>

      <p class="para">
       The flags that you can use:
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara">
          &quot;a&quot; for access (sets SHM_RDONLY for shmat)
          use this flag when you need to open an existing shared memory
          segment for read only
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          &quot;c&quot; for create (sets IPC_CREATE)
          use this flag when you need to create a new shared memory segment
          or if a segment with the same key exists, try to open it for read
          and write
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          &quot;w&quot; for read &amp; write access
          use this flag when you need to read and write to a shared memory
          segment, use this flag in most cases.
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          &quot;n&quot; create a new memory segment (sets IPC_CREATE|IPC_EXCL)
          use this flag when you want to create a new shared memory segment
          but if one already exists with the same flag, fail. This is useful
          for security purposes, using this you can prevent race condition
          exploits.
         </span>
        </li>
       </ul>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">permissions</code></dt>

     <dd>

      <p class="para">
       The permissions that you wish to assign to your memory segment, those
       are the same as permission for a file. Permissions need to be passed
       in octal form, like for example <code class="literal">0644</code>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">size</code></dt>

     <dd>

      <p class="para">
       The size of the shared memory block you wish to create in bytes
      </p>
     </dd>

    
   </dl>

   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
     Note: the 3rd and 4th should be entered as 0 if you are opening an
     existing memory segment.
    </p>
   </p></blockquote>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.shmop-open-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   On success <span class="function"><strong>shmop_open()</strong></span> will return a <span class="classname"><a href="class.shmop.html" class="classname">Shmop</a></span> instance that you can
   use to access the shared memory segment you&#039;ve created. <strong><code>false</code></strong> is
   returned on failure.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.shmop-open-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       On success, this function returns an <span class="classname"><a href="class.shmop.html" class="classname">Shmop</a></span> instance now;
       previously, a <span class="type">resource</span> was returned.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.shmop-open-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3352">
    <p><strong>示例 #1 Create a new shared memory block</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$shm_key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">ftok</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'t'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$shm_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">shmop_open</span><span style="color: #007700">(</span><span style="color: #0000BB">$shm_key</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"c"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0644</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   This example opened a shared memory block with a system id returned by
   <span class="function"><a href="function.ftok.html" class="function">ftok()</a></span>.
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.shmop-open-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.shmop-close.html" class="function" rel="rdfs-seeAlso">shmop_close()</a> - Close shared memory block</span></li>
    <li class="member"><span class="function"><a href="function.shmop-delete.html" class="function" rel="rdfs-seeAlso">shmop_delete()</a> - Delete shared memory block</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>